import * as XLSX from 'xlsx';
import { formatData } from "@/utils/excelFormatData"
import { message } from 'antd';


export function xlsxFormate(file: any, map: any) {


    return new Promise((resolve, reject) => {
        let reader = new FileReader();
        (reader as any).readAsBinaryString(file)

        reader.onload = (event) => {
            let { result } = event.target as any
            try {
                // 读取文件
                let workbook = XLSX.read(result, {
                    type: 'binary'
                });
                let data: Array<any> = []
                // 循环文件中的每个表
                for (let sheet in workbook.Sheets) {

                    if (workbook.Sheets.hasOwnProperty(sheet)) {

                        // 将获取到表中的数据转化为json格式
                        data = data.concat(XLSX.utils.sheet_to_json(workbook.Sheets[sheet]));
                    }
                }
                resolve(formatData(data, map))

            } catch (e) {
                message.error('文件类型不正确！');
                reject("导入失败")
            }

        }

    })
}



function callBack(data: Array<any> = []) {
    return data
}